﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

namespace LockScreenSwitcher
{
    static class IoHelper
    {
        [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr oldValue);

        [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr oldValue);

        public static bool FileExists(string path)
        {
            if (File.Exists(path)) return true;
            IntPtr oldValue = IntPtr.Zero;
            try
            {
                if (Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432") == null)
                    return false;

                Wow64DisableWow64FsRedirection(ref oldValue);
                if (File.Exists(path)) return true;

                return false;
            }
            finally
            {
                if (oldValue != IntPtr.Zero)
                    Wow64RevertWow64FsRedirection(oldValue);
            }
        }

        public static bool DirectoryExists(string path)
        {
            if (File.Exists(path)) return true;
            IntPtr oldValue = IntPtr.Zero;
            try
            {
                if (Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432") == null)
                    return false;

                Wow64DisableWow64FsRedirection(ref oldValue);
                if (Directory.Exists(path)) return true;

                return false;
            }
            finally
            {
                if (oldValue != IntPtr.Zero)
                    Wow64RevertWow64FsRedirection(oldValue);
            }
        }

        public static void CreateDirectory(string path)
        {
            try
            {
                Directory.CreateDirectory(path);
            }
            catch (Exception e)
            {
                if (Directory.Exists(path)) return;

                IntPtr oldValue = IntPtr.Zero;
                try
                {
                    Wow64DisableWow64FsRedirection(ref oldValue);
                    Directory.CreateDirectory(path);
                }
                finally
                {
                    if (oldValue != IntPtr.Zero)
                        Wow64RevertWow64FsRedirection(oldValue);
                }
            }
        }

        public static IEnumerable<FileInfo> GetFilesByExtensions(this DirectoryInfo dir, params string[] extensions)
        {
            if (extensions == null)
                throw new ArgumentNullException("extensions");
            IEnumerable<FileInfo> files = dir.EnumerateFiles();
            return files.Where(f => extensions.Contains(f.Extension));
        }

        public static string ExpandEnvironmentAndSpecialFolderPaths(string path)
        {
            string fullPath = (string)path.Clone();

            //// Replace Environment.SpecialFolder types:
            //foreach (string specialFolderName in Enum.GetNames(typeof(Environment.SpecialFolder)))
            //{
            //    Environment.SpecialFolder specialFolder = (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), specialFolderName);
            //    switch (specialFolder)
            //    {
            //        case Environment.SpecialFolder.AdminTools:
            //            break;
            //        case Environment.SpecialFolder.ApplicationData:
            //            break;
            //        case Environment.SpecialFolder.CDBurning:
            //            break;
            //        case Environment.SpecialFolder.CommonAdminTools:
            //            break;
            //        case Environment.SpecialFolder.CommonApplicationData:
            //            break;
            //        case Environment.SpecialFolder.CommonDesktopDirectory:
            //            break;
            //        case Environment.SpecialFolder.CommonDocuments:
            //            break;
            //        case Environment.SpecialFolder.CommonMusic:
            //            break;
            //        case Environment.SpecialFolder.CommonOemLinks:
            //            break;
            //        case Environment.SpecialFolder.CommonPictures:
            //            break;
            //        case Environment.SpecialFolder.CommonProgramFiles:
            //            break;
            //        case Environment.SpecialFolder.CommonProgramFilesX86:
            //            break;
            //        case Environment.SpecialFolder.CommonPrograms:
            //            break;
            //        case Environment.SpecialFolder.CommonStartMenu:
            //            break;
            //        case Environment.SpecialFolder.CommonStartup:
            //            break;
            //        case Environment.SpecialFolder.CommonTemplates:
            //            break;
            //        case Environment.SpecialFolder.CommonVideos:
            //            break;
            //        case Environment.SpecialFolder.Cookies:
            //            break;
            //        case Environment.SpecialFolder.Desktop:
            //            break;
            //        case Environment.SpecialFolder.DesktopDirectory:
            //            break;
            //        case Environment.SpecialFolder.Favorites:
            //            break;
            //        case Environment.SpecialFolder.Fonts:
            //            break;
            //        case Environment.SpecialFolder.History:
            //            break;
            //        case Environment.SpecialFolder.InternetCache:
            //            break;
            //        case Environment.SpecialFolder.LocalApplicationData:
            //            break;
            //        case Environment.SpecialFolder.LocalizedResources:
            //            break;
            //        case Environment.SpecialFolder.MyComputer:
            //            break;
            //        case Environment.SpecialFolder.MyDocuments:
            //            break;
            //        case Environment.SpecialFolder.MyMusic:
            //            break;
            //        case Environment.SpecialFolder.MyPictures:
            //            break;
            //        case Environment.SpecialFolder.MyVideos:
            //            break;
            //        case Environment.SpecialFolder.NetworkShortcuts:
            //            break;
            //        case Environment.SpecialFolder.PrinterShortcuts:
            //            break;
            //        case Environment.SpecialFolder.ProgramFiles:
            //            break;
            //        case Environment.SpecialFolder.ProgramFilesX86:
            //            break;
            //        case Environment.SpecialFolder.Programs:
            //            break;
            //        case Environment.SpecialFolder.Recent:
            //            break;
            //        case Environment.SpecialFolder.Resources:
            //            break;
            //        case Environment.SpecialFolder.SendTo:
            //            break;
            //        case Environment.SpecialFolder.StartMenu:
            //            break;
            //        case Environment.SpecialFolder.Startup:
            //            break;
            //        case Environment.SpecialFolder.System:
            //            break;
            //        case Environment.SpecialFolder.SystemX86:
            //            break;
            //        case Environment.SpecialFolder.Templates:
            //            break;
            //        case Environment.SpecialFolder.UserProfile:
            //            break;
            //        case Environment.SpecialFolder.Windows:
            //            break;
            //        default:
            //            break;
            //    }
            //    fullPath = fullPath.Replace("%" + specialFolder + "%", Environment.GetFolderPath(specialFolder));
            //}
            fullPath = Environment.ExpandEnvironmentVariables(fullPath);

            return fullPath;
        }
    }
}
